import com.dmw.dao.IUserDaoRepository01;
import com.dmw.pojo.UserTrans;
import com.dmw.pojo.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IUsersDaoRepositoryTest01 {
    @Autowired
    private IUserDaoRepository01 userDao;

    @Test
    @Transactional
    @Rollback(false)
    public void findByUserName() {
        List<Users> usersList = userDao.findByUserNameIs("浮萍");
        //lambda 遍历结果
        usersList.forEach(user -> {
            System.out.println("查询结果： " + user.toString());
        });
        //lambda 转化实体类
          List <UserTrans> transList = usersList.stream().map( user -> { UserTrans trans = new UserTrans();
                      trans.setUserIdTrans(user.getUserId());
                      trans.setUserNameTrans(user.getUserName());
                      trans.setRealNameTrans(user.getRealName());
                      return trans; } )
                .collect( Collectors.toList()  );
        System.out.println("新的实体类UserTrans ： "+ Arrays.toString(transList.toArray()));

        transList =  transList.stream().distinct().collect( Collectors.toList()  );
        System.out.println("去重实体类UserTrans ： "+ Arrays.toString(transList.toArray()));

        transList =  transList.stream().sorted(Comparator.comparing(String ::valueOf) ).collect( Collectors.toList()  );
        System.out.println("正序排序实体类UserTrans ： "+ Arrays.toString(transList.toArray()));

        transList =  transList.stream().sorted(Comparator.comparing(String :: valueOf , Comparator.reverseOrder()) ).collect(Collectors.toList());
        System.out.println("倒序排序实体类UserTrans ： "+ Arrays.toString(transList.toArray()));
    }

    @Test
    @Transactional
    @Rollback(false)
    public void findByUserNameLike() {
        List<Users> usersList = userDao.findByUserNameLike("%虎");
        usersList.forEach( tiger -> {
            System.out.println( "查询结果： " + tiger.toString() );
        }  );
    }

    @Test
    @Transactional
    @Rollback(false)
    public void findByUserNameAndRealName() {
        List <Users> usersList = userDao.findByUserNameAndRealName("猛虎","胡八一");
        usersList.forEach( u -> {
            System.out.println("UserId值为： " +  u.getUserId()  );
            System.out.println("UserName值为： " +  u.getUserName() );
            System.out.println("RealName值为： " +  u.getRealName() );
        }  );
    }


}
